package com.ht.controller;

import com.ht.data.Result;
import com.ht.domain.bo.ArticleBO;
import com.ht.domain.po.Article;
import com.ht.dto.ArticleDTO;
import com.ht.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("articleController")
@RefreshScope
@Api
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章并绑定科普号id
     * @param popularizationId 科普号id
     * @param articleDTO 科普文章实体
     * @return
     */
    @ApiOperation("添加文章并绑定科普号id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "popularizationId",value = "科普号id",required = true,paramType = "path",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "articleDTO",value = "科普文章实体",required = true,paramType = "body",dataTypeClass = ArticleDTO.class)
    })
    @PostMapping("addArticle/{popularizationId}")
    public Result addArticle(@PathVariable("popularizationId")Long popularizationId, @RequestBody ArticleDTO articleDTO){
        ArticleBO articleBO = new ArticleBO();
        BeanUtils.copyProperties(articleDTO,articleBO);
        int i = articleService.addArticle(popularizationId,articleBO);
        if (i <= 0){
            return Result.ERROR();
        }
        return Result.OK();
    }
}
